package com.code.leetcode._202504;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**电话号码的字母组合**/
public class PhoneNumberArr {
    public static void main(String[] args) {
        letterCombinations("7");
    }
    static Map<String, String[]> map = new HashMap<>(){{
        put("2", new String[]{"a", "b", "c"});
        put("3", new String[]{"d", "e", "f"});
        put("4", new String[]{"g", "h", "i"});
        put("5", new String[]{"j", "k", "l"});
        put("6", new String[]{"m", "n", "o"});
        put("7", new String[]{"p", "q", "r", "s"});
        put("8", new String[]{"t", "u", "v"});
        put("9", new String[]{"w", "x", "y", "z"});
    }};

    public static List<String> letterCombinations(String digits) {
        int len = digits.length();
        List<String> ans = new ArrayList<>();
        if (len == 0) return ans;
        StringBuilder sb = new StringBuilder();
        dfs(digits,0,len,sb,ans);
        return ans;
    }

    public static void dfs(String digits,int i,int n,StringBuilder sb,List<String> ans){
        if (i == n){
            ans.add(sb.toString());
            return;
        }
        String key = digits.substring(i,i+1);
        String[] all = map.get(key);
        for (String item : all) {
            sb.append(item);
            dfs(digits,i+1,n,sb,ans);
            sb.deleteCharAt(sb.length()-1);
        }
    }


}
